﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using Ajax.BLL;
using Newtonsoft.Json;

namespace Habito.Helper
{
	public class DataHelper
	{
		public static string GetAllProductClass()
		{
			ProductClass products = new ProductClass();
			List<Ajax.Model.ProductClass> list = products.GetModelList("");
			StringBuilder temp = new StringBuilder();
			foreach (var item in list)
			{
				if (item.Name.Length > 30)
				{
					item.Name = item.Name.Substring(0, 30) + "...";
				}
			}
			StringBuilder tmep2 = new StringBuilder();
			List<Ajax.Model.ProductClass> tempList = new List<Ajax.Model.ProductClass>();
			foreach (var item in list)
			{
				if (item.PID == 0)
				{
					tempList.Add(item);
				}
			}
			foreach (var item in tempList)
			{
				tmep2.AppendFormat("<option value='{1}'>{0}</option>", item.Name, item.ID);
				string temps = @"<li><span classID='{1}' tt='1'>{0}</span><ul>{2}</ul></li>";
				StringBuilder tt = new StringBuilder();
				List<Ajax.Model.ProductClass> tempLists = new List<Ajax.Model.ProductClass>();
				foreach (var itms in list)
				{
					if (itms.PID == item.ID)
					{
						tempLists.Add(itms);
					}
				}
				foreach (var c in tempLists)
				{
					tt.AppendFormat("<li classID='{1}' tt='1'>{0}</li>", c.Name, c.ID);
				}
				temps = string.Format(temps, item.Name, item.ID, tt.ToString());
				temp.Append(temps);
			}
			return temp.ToString();
		}
		public static string GetProductFirstClass()
		{
			ProductClass products = new ProductClass();
			List<Ajax.Model.ProductClass> list = products.GetModelList("");
			foreach (var item in list)
			{
				if (item.Name.Length > 30)
				{
					item.Name = item.Name.Substring(0, 30) + "...";
				}
			}
			StringBuilder tmep2 = new StringBuilder();
			List<Ajax.Model.ProductClass> tempList = new List<Ajax.Model.ProductClass>();
			foreach (var item in list)
			{
				if (item.PID == 0)
				{
					tempList.Add(item);
				}
			}
			foreach (var item in tempList)
			{
				tmep2.AppendFormat("<option value='{1}'>{0}</option>", item.Name, item.ID);
			}
			return tmep2.ToString();
		}

		public static string GetAll()
		{
			ProductClass products = new ProductClass();
			List<Ajax.Model.ProductClass> list = products.GetModelList("");
			foreach (var item in list)
			{
				if (item.Name.Length > 30)
				{
					//item.Name = item.Name.Substring(0, 30) + "...";
				}
			}
			List<ProductClassExtension> lists = new List<ProductClassExtension>();
			foreach (var item in list)
			{
				if (item.PID == null)
				{
					var aa = new ProductClassExtension() { text = item.Name, id = item.ID };
					lists.Add(aa);
					aa.children = new List<ProductClassExtension>();
					foreach (var sss in list)
					{
						if (sss.PID == item.ID)
						{
							aa.children.Add(new ProductClassExtension() { id = sss.ID, text = sss.Name });
						}
					}
				}
			}
			return JsonConvert.SerializeObject(lists);
		}
	}

	class ProductClassExtension
	{
		public string text { get; set; }
		public int id { get; set; }
		public List<ProductClassExtension> children { get; set; }
	}
}